package Agency.models;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import Agency.views.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author wangari
 */
public class AllTenantsController  extends javax.swing.JFrame{
    Connection conn = null;
   public AllTenantsView myTenantsView;
   private ActionListener actionlistener;
    ArrayList<Tenant> tenantsArrayList =  new ArrayList<Tenant>();
   //Tenant myNewTenant = new 
   
  /**
   * A constructor that instantiates a new object of the all tenants view
   */
    public AllTenantsController(){
     myTenantsView= new  AllTenantsView(); 
    //myTenantsView.setVisible(true);
   }
    /**
     * Displaying all the tenants in a default table
     */
   
    public void displayAllTenants(){
     
    
               try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its not connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT * FROM TenantsInformation");

  while(r.next()) {
     Object [] myArray ={r.getString("TenantsName"),r.getString("TenantsIdNo"),r.getString("TenantsPhoneNumber"),r.getString("HouseType"),r.getString("HouseType"),r.getString("HouseId")+"",""
              + r.getString("Town"),r.getString("Gender"),r.getString("Occupation")};
        
      myTenantsView.myTenantsTable().addRow(myArray);
      Tenant myNewTenant = new Tenant(r.getString("TenantsName"),r.getString("TenantsIdNo"),r.getString("TenantsPhoneNumber"),r.getString("HouseType"),
                                               r.getString("HouseType"),r.getString("Town"),r.getString("Gender"),r.getString("Occupation"));
  
      tenantsArrayList .add(myNewTenant);
  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
    }
  /**
   * The method exports data from the ArrayList to the text file chosen by the user
   */             
public void exportingDataToTextFile(){
     String myTextDoc=null; 
        JFrame myframe =new JFrame();
        JFileChooser myChooser = new JFileChooser();
        myChooser.setDialogTitle("Specify where you want to save");
        
        int fileSelected =myChooser .showSaveDialog(myframe);
        
        if(fileSelected ==JFileChooser.APPROVE_OPTION){
            File savingFile =myChooser.getSelectedFile();
            System.out.println("Save as file" +savingFile.getAbsolutePath());
            myTextDoc = savingFile.getAbsolutePath();
        }
        
       try {
            PrintWriter myWriter = new PrintWriter (new FileOutputStream (myTextDoc,true));
           //AllTenantsController allTheTenants = new AllTenantsController();

                for(int i=0; i<tenantsArrayList.size();i++){
                    System.out.println(tenantsArrayList.get(i));
                    myWriter.println(tenantsArrayList.get(i));
                }
                JOptionPane.showMessageDialog (null, "Tenants records have been saved succesfully to file" , "Save to File", JOptionPane.INFORMATION_MESSAGE);
                myWriter.close();
               
            }
        catch (IOException e) {
            System.err.println("FileStreamsTest: " + e);
    }                       
    }
   /**
    * Closing the view
    */
    //}
    public void closeView(){
     myTenantsView.setVisible(false);   
        
    }
}
